<?php

class albumDao {

    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function create(AlbumClass $album) {
        $db = $this->db;
        $stmt = $db->prepare("INSERT INTO album (name_album,userid,dateinclusion,photo_capa, directory ) VALUES (?,?,?, ?, ?)");

        $albumName = $album->getNameAlbum();
        $stmt->bindParam(1, $albumName);
        
        $user = $album->getIdUser();
        $stmt->bindParam(2, $user);

        $dateInclusion = $album->getDateInclusionAlbum();
        $stmt->bindParam(3, $dateInclusion);

        $photoCapa = $album->getPhotoCapa();
        $stmt->bindParam(4, $photoCapa);

        $directory = $album->getDirectory();
        $stmt->bindParam(5, $directory);
        return $stmt->execute();
    }

    public function delete($idAlbum) {

        $db = $this->db;
        $stmt = $db->prepare("delete FROM album  where id=$idAlbum");
        return $stmt->execute();
    }

    public function removeAll() {

        $db = $this->db;
        $stmt = $db->prepare("delete FROM album");
        return $stmt->execute();
    }

    public function edite() {
        
    }

    /*public function getById($id) {

        $db = $this->db;
        $stmt = $db->prepare("SELECT * FROM  `album` where id=$id");

        $stmt->execute();
        $row = $stmt->fetch();
        $i = array();
        if (isset($row['id'])) {

            $album = new AlbumClass();
            $album->setIdAlbum($row['id']);
            $album->setUser($row['user']);
            $album->setNameAlbum($row['name_album']);
            $album->setPhotoCapa($row['photo_capa']);
            $album->setDirectory($row['directory']);
            $album->setDateInclusionAlbum($row['dateinclusion']);
            $albuns[] = $album;
            return  $albuns;
            ;
        }
    }*/

    Public function getAlbumByName($album) {

        $db = $this->db;
        $stmt = $db->prepare("SELECT * FROM  `album` where photo_capa='?'");
        $stmt->bindParam(1, $album);
        $stmt->execute();
        $row = $stmt->fetch();


        if (isset($row['id'])) {
            $album = new AlbumClass();
            $album->setIdAlbum($row['id']);
            $album->setIdUser($row['userid']);
            $album->setNameAlbum($row['name_album']);
            $album->setDateInclusionAlbum($row['dateinclusion']);
            $album->setPhotoCapa($row['photo_capa']);
            $album->setDirectory($row['directory']);
            return $album;
        }
        return false;
    }

    Public function getIdAlbumByName($album) {

        echo '<br>Pesquisando por :' . $album;
        $db = $this->db;
        $stmt = $db->prepare("SELECT * FROM  album a where a.photo_capa='$album'");

        $stmt->execute();
        $row = $stmt->fetch();
        $id=false;
        if (isset($row['id'])) {
            $id = ($row['id']);
            return $id;
        }
        return $id;
    }

    Public function getAlbuns($userid,$albumid) {

        $albumArray = Array();
        $db = $this->db;

        if ($albumid == '') {
            $stmt = $db->prepare("SELECT a.*,u.user FROM `album` a join users u on u.id=a.userid where a.userid=$userid");
        } else {
            $stmt = $db->prepare("SELECT a.*,u.user FROM `album` a join users u on u.id=a.userid where a.id=$albumid and a.userid=$userid");
        }
        $stmt->execute();
        $result = $stmt->fetchAll();

        if (sizeof($result) > 0) {
            foreach ($result as $row) {
                $album = new AlbumClass();
                $album->setIdAlbum($row['id']);
                $album->setIdUser($row['userid']);
                $album->setNameUser($row['user']);
                $album->setNameAlbum($row['name_album']);
                $album->setDateInclusionAlbum($row['dateinclusion']);
                $album->setPhotoCapa($row['photo_capa']);
                $album->setDirectory($row['directory']);
                $albumArray[] = $album;
            }

            return $albumArray;
        }
        else
            return false;
    }

}

?>
